<?php

namespace addons\official\controller\web;

use addons\official\model\OfficialBanners;
use addons\official\model\OfficialCategory;
use think\addons\Controller;
use think\Cache;

class OfficialBaseController extends Controller
{

    public $basic = [];
    public $language_z = '';
    protected $language = 2;
    protected $basic_title;
    protected $keywords;
    protected $description;
    protected $Mobile = '';

    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        $this->language = (int)cookie('language') ? (int)cookie('language') : 2;
        $this->assign('language', $this->language);
        $this->assign('auth', $this->auth);
        //判断移动PC端
        if ($this->isMobile()) {
            $this->Mobile = '_e';
        }
        //查询所有栏目
        $this->getAll0fficiaCategory();
        //查询轮播图
        $this->getAllBanners();
        //网站名称 关键词 介绍
        $this->language_z = ($this->language == 1) ? '_en' : '';


        //生成配置文件
        $official_config_url = ADDON_PATH . 'official' . DS . 'controller' . DS . 'common' . DS . 'official_config.php';
        if (!file_exists($official_config_url)) {
            $config_all = [];
            $data = db('official_config')->select();
            foreach ($data as $item) {
                $config_all[$item['name']] = $item;
            }
            file_put_contents(
                $official_config_url,
                '<?php' . "\n\nreturn " . var_export_short($config_all) . ";\n"
            );
        } else {
            $config_all = include $official_config_url;
        }
        $this->basic_title = $config_all['basic_title' . $this->language_z]['value'] ?? '';
        $this->keywords = $config_all['keywords' . $this->language_z]['value'] ?? '';
        $this->description = $config_all['description' . $this->language_z]['value'] ?? '';

        $this->basic = [
            'title'       => $this->basic_title,
            'keywords'    => $this->keywords,
            'description' => $this->description,
        ];
        $this->view->assign('captchaType', config('fastadmin.user_register_captcha'));
        $this->assign('basic', $this->basic);
    }

    /**
     * @return bool
     * 移动pc判断
     */
    public function isMobile()
    {
        return request()->isMobile();
    }

    /**
     * 查询所有栏目
     */
    public function getAll0fficiaCategory()
    {
        //生成配置文件
        $official_tree_category_url = ADDON_PATH . 'official' . DS . 'controller' . DS . 'common' . DS . 'official_tree_category.php';
        if (!file_exists($official_tree_category_url)) {
            $official_category = (new OfficialCategory())
                ->with(['son'])
                ->where('pid', 0)
                ->field(['content', 'e_content'], true)
                ->where('deletetime', null)
                ->order('weigh', 'desc')
                ->select();
            $tree_category_all = [];
            foreach ($official_category as $item) {
                $tree_category_all[] = $item->toArray();
            }
            file_put_contents(
                $official_tree_category_url,
                '<?php' . "\n\nreturn " . var_export_short($tree_category_all) . ";\n"
            );
        } else {
            $tree_category_all = include $official_tree_category_url;
        }
        $this->assign('all_official_category', $tree_category_all);
    }

    /**
     * 获取所有轮播图
     */
    public function getAllBanners()
    {
        $cid = (int)input('param.sid');
        $banners = (new OfficialBanners())
            ->where('status', 1)
            ->where('language', $this->language)
            ->where('deletetime', null);
        //栏目id
        if ($cid) {
            $banners = $banners->where("FIND_IN_SET(" . $cid . ",official_category_ids)");
        } else {
            $banners = $banners->where('is_index', 1);
        }
        //查询
        $banners = $banners
            ->order('weigh', 'desc')
            ->select();
        $this->assign('banners', $banners);
    }

    /**
     * 获取栏目数据
     * @return array|bool|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function get_cate_pid_in_sid()
    {
        $sid = (int)input('param.sid');
        $pid = (int)input('param.pid');
        //1.查詢父级
        $res = [];
        $official_tree_category_url = ADDON_PATH . 'official' . DS . 'controller' . DS . 'common' . DS . 'official_tree_category.php';
        if (file_exists($official_tree_category_url)) {
            $tree_category_all = include $official_tree_category_url;
            foreach ($tree_category_all as $item) {
                if ($item['id'] == $pid) {
                    $res = $item;
                }
            }
        }
        //2.判断是否选中
        if (isset($res['son'])) {
            foreach ($res['son'] as $key => $item) {
                //如果不在导航中显示 则删除
                if ($item['is_nav_show'] != 1) {
                    unset($res['son'][$key]);
                    continue;// 删除后，跳出本次循环
                }
                //判断是否选中
                $res['son'][$key]['is_choose'] = 0;
                if (!empty($sid)) {
                    if ($item['id'] == $sid) {
                        $res['son'][$key]['is_choose'] = 1;
                    }
                }
            }
        }
        return $res;
    }

    /**
     *增加点击量
     * @param $model 模型
     * @param $id
     */
    protected function addClick($model, $id)
    {
        $model->where('id', $id)->setInc('click');
    }

    /**
     * 设置 网站名称 关键词 介绍
     * @param int    $stu stu == 1 代表是栏目  stu == 2 代表详情
     * @param string $title
     * @param string $keywords
     * @param string $description
     */
    protected function setBasic($stu = 1, $title = '', $keywords = '', $description = '')
    {
        $arr = ['title' => $title, 'keywords' => $keywords, 'description' => $description];
        foreach ($arr as $item => $val) {
            if ($val) {
                $this->basic[$item] = $val;
            }
        }
        if ($stu == 1) {
            $language_z = ($this->language == 1) ? 'e_' : '';
            $category = db('official_category')->where('id', (int)input('param.sid'))->find();
            if ($category) {
                $this->basic = [
                    'title'       => $this->basic_title . '|' . $category[$language_z . 'name'],
                    'keywords'    => $this->keywords,
                    'description' => $category[$language_z . 'subheading'],
                ];
            }
        }
        $this->assign('basic', $this->basic);
    }


}
